This notebook will demonstrate the process of splitting a video into its component frames for an object recognition model.
Necessary libraries:
library(gdata)
library("jpeg")
library("imager")
With the videos captured, we use the following functions to convert them into images:
getFileNameAndExtension <- function(file){
ex <- strsplit(basename(file), split="\\.")
ex<-unlist(ex)
return(ex)
}
convertVideoAsImages<-function(ffmpeg,videoFileName,fps,filePrefix,fileSuffix)
{
nameAndExtension<-getFileNameAndExtension(videoFileName)
utils::browseURL(videoFileName)
dir.create(nameAndExtension[[1]])
command<-sprintf("%s -i %s -vf fps=%d ./%s/%s%%d%s",ffmpeg,videoFileName,fps,nameAndExtension[[1]],filePrefix,fileSuffix)
command
system(command)
}
Now we input our file name and settings and convert the video into frames:
firstVideoFileName<-'marker.mov'
ffmpeg<-'c:/ffmpeg/bin/ffmpeg'
outFilePrefix='out'
outFileSuffix='.jpg'
fps=10
convertVideoAsImages(ffmpeg,firstVideoFileName,fps,outFilePrefix,outFileSuffix)
Here is an example frame from the video to ddemonstrate that the process was successfully performed:
nameAndExtension<-getFileNameAndExtension(firstVideoFileName)
fnames <- paste0(nameAndExtension[[1]],"/",outFilePrefix, 1, outFileSuffix)
jj <- readJPEG(fnames,native=TRUE)
plot(0:1,0:1,type="n",ann=FALSE,axes=FALSE)
rasterImage(jj,0,0,1,1)
We repeat the process for a second video:
secondVideoFileName<-'bottle.mov'
convertVideoAsImages(ffmpeg,secondVideoFileName,fps,outFilePrefix,outFileSuffix)
nameAndExtension<-getFileNameAndExtension(secondVideoFileName)
fnames <- paste0(nameAndExtension[[1]],"/",outFilePrefix, 1, outFileSuffix)
jj <- readJPEG(fnames,native=TRUE)
plot(0:1,0:1,type="n",ann=FALSE,axes=FALSE)
rasterImage(jj,0,0,1,1)
We use the following code to reduce the image size for classification, reducing the image to grayscale for simplicity:
compressRatio=40
nameAndExtension<-getFileNameAndExtension(firstVideoFileName)
fnames <- paste0(nameAndExtension[[1]],"/",outFilePrefix, 100, outFileSuffix)
sampleImage <- load.image(fnames)
plot(sampleImage)
thmb <- resize(sampleImage,round(width(sampleImage)/compressRatio),round(height(sampleImage)/compressRatio))
gs<-grayscale(thmb)
plot(gs)
gs.matrix<-as.matrix(gs)
dim(gs.matrix)
plot(thmb,main="Thumbnail")
We use the following function to save the reduced images in their own folder:
reduceImagesfromFolder<-function(reductionRatio,thumbNailFolderPrefix,imageFolder,prefix)
{
imageNames<-paste0(prefix,"*.*")
files<-list.files(path=imageFolder,pattern=imageNames,all.files=T,full.name=T,no..=T)
list_of_images=lapply(files,load.image)
n.images<-length(list_of_images)
outFolderName<-sprintf("%s%s",thumbNailFolderPrefix,imageFolder)
outFileName<-sprintf("%s%s",thumbNailFolderPrefix,files[[1]])
if(!dir.exists(outFolderName))
{
dir.create(path=outFolderName)
}
for(ii in 1:n.images)
{
outFileName<-sprintf("%s%s",thumbNailFolderPrefix,files[[ii]])
thmb <- resize(list_of_images[[ii]],round(width(list_of_images[[ii]])/reductionRatio),round(height(list_of_images[[ii]])/reductionRatio))
thmb<-grayscale(thmb)
imager::save.image(thmb,outFileName)
}
}
We call the function on the first image set and plot an example:
thumbNailFolder<-"Small"
nameAndExtension<-getFileNameAndExtension(firstVideoFileName)
reduceImagesfromFolder(compressRatio,thumbNailFolder,nameAndExtension[[1]],outFilePrefix)
markerex<-load.image("smallmarker/out1.jpg")
plot(markerex)
We call the function on the second image set and plot an example:
nameAndExtension<-getFileNameAndExtension(secondVideoFileName)
reduceImagesfromFolder(compressRatio,thumbNailFolder,nameAndExtension[[1]],outFilePrefix)
bottlex<-load.image("smallbottle/out1.jpg")
plot(bottlex)